/*
 * @Author: hucc
 * @Date: 2021-10-15 17:01:18
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-17 15:29:46
 * @Description: 
 */
//1.设置全局默认的配置项
axios.defaults.baseURL = "http://39.103.202.53:8888";
//2.设置请求拦截器
axios.interceptors.request.use(config => {
    // Do something before request is sent
    let token = sessionStorage.getItem('token');
    if (config.url != '/user/login' && token) {
        config.headers['Authorization'] = token;
    }
    return config;
}, error => {
    // Do something with request error
    return Promise.reject(error);
});
//3.设置响应拦截器
axios.interceptors.response.use(response => {
    // Do something before response is sent
    response = {
        ...response,
        data: response.data.data,
        status: response.data.status,
        statusText: response.data.message
    }
    return response;
}, error => {
    // Do something with response error
    return Promise.reject(error);
});
let qs = Qs;
//4.封装请求方法

//封装 post方法 表头为表单数据  所以得用查询字符串
function post_form(url, data) {
    return axios({
        url,
        method: 'post',
        data: qs.stringify(data),
        headers: {
            'Content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        }
    })
}
//封装 post方法 表头为查询字符串格式   所以得用json格式数据   axios中post方法默认支持json格式
function post_json(url, data) {
    return axios({
        url,
        method: 'post',
        data: JSON.stringify(data)
    })
}
//封装get函数方法
function my_get(url, params) {
    return axios({
        url,
        method: 'get',
        params: qs.stringify(params)
    })
}